package mycoffee.order.dialog;

import mycoffee.order.R;
import mycoffee.order.model.Order;
import mycoffee.order.utils.CommonUtils;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.print.ESCPSample2;

public class PaymentDialog extends Dialog implements OnClickListener,
		TextWatcher {

	private double mTotalMoney;
	private ImageView mIvDelete;
	private TextView mTvTotalMoney;
	private EditText mEtCustomerGiveMoney;
	private TextView mTvExcessMoney;
	private boolean mIsPrintBill = false;
	private Order mOrder;
	private Button btPrint;

	public PaymentDialog(Context context, double totalMoney, Order order) {
		super(context);
		mTotalMoney = totalMoney;
		mOrder = order;
	}

	public void reset(Order order, double totalMoney) {
		mOrder = order;
		mTotalMoney = totalMoney;
		mEtCustomerGiveMoney.setText("");
		mTvExcessMoney.setText("");
		mTvTotalMoney.setText(""
				+ CommonUtils.convertMoneyToString(mTotalMoney)
				+ getContext().getString(R.string.money_unit));
		mIsPrintBill = false;
		btPrint.setEnabled(false);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.payment_dialog);
		mIvDelete = (ImageView) findViewById(R.id.payment_dialog_iv_delete);
		mIvDelete.setOnClickListener(this);
		btPrint = (Button) findViewById(R.id.payment_dialog_bt_print_bill);
		btPrint.setOnClickListener(this);
		btPrint.setEnabled(false);
		Button btCancel = (Button) findViewById(R.id.payment_dialog_bt_cancel);
		btCancel.setOnClickListener(this);
		mTvTotalMoney = (TextView) findViewById(R.id.payment_dialog_tv_total_value);
		mTvTotalMoney.setText(""
				+ CommonUtils.convertMoneyToString(mTotalMoney)
				+ getContext().getString(R.string.money_unit));

		mEtCustomerGiveMoney = (EditText) findViewById(R.id.payment_dialog_et_customer_give_value);
		mEtCustomerGiveMoney.addTextChangedListener(this);

		mTvExcessMoney = (TextView) findViewById(R.id.payment_dialog_tv_excess_money);
		super.onCreate(savedInstanceState);
	}

	@Override
	public void onClick(View arg0) {
		if (arg0 == null) {
			return;
		}
		switch (arg0.getId()) {
		case R.id.payment_dialog_bt_print_bill:
			mIsPrintBill = true;
			if (mOrder != null) {		
				ESCPSample2 sample = new ESCPSample2(mOrder);
				try {
					sample.sample1();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			} else {
				ESCPSample2 sample = new ESCPSample2();
				try {
					sample.sample1();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			dismiss();
			break;
		case R.id.payment_dialog_bt_cancel:
		case R.id.payment_dialog_iv_delete:
		default:
		}
		dismiss();
	}
	
	@Override
	public void afterTextChanged(Editable arg0) {

	}

	@Override
	public void beforeTextChanged(CharSequence s, int start, int count,
			int after) {

	}

	@Override
	public void onTextChanged(CharSequence s, int start, int before, int count) {
		// calculate exceed money.
		double exceedMoney = 0;
		if (!TextUtils.isEmpty(s)) {
			exceedMoney = Double.parseDouble(s.toString()) - mTotalMoney;
		}
		mTvExcessMoney.setText(CommonUtils.convertMoneyToString(exceedMoney)
				+ getContext().getString(R.string.money_unit));
		
		if ( exceedMoney >= 0 ) {
			btPrint.setEnabled(true);
		} else {
			btPrint.setEnabled(false);
		}
	}

	public boolean isPrintBill() {
		return mIsPrintBill;
	}

}
